home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Libraries / BlobMgr / Blob Manager Demo 4 / DemoStates.c next >
Encoding:
C/C++ Source or Header  |  1987-02-17  |  4.8 KB  |  208 lines  |  [TEXT/MACA]

  1. /*
  2.     Blob Manager Demonstration:  States and Capitals module
  3.  
  4.     This module is an example of an extremely simple interrogative
  5.     scenario.  The original thirteen colonies of the United States must
  6.     be matched with their capital cities.  There is a single button that
  7.     says "Give Up?"  If the user clicks the button, the answer is shown,
  8.     the button's title changes to "Resume", and the scenario is frozen
  9.     until the button is clicked.  Then the answer is cleared, the capitals
  10.     are shuffled, and the user may start over.
  11.  
  12.     If the correct answers are all gotten, the button again changes to
  13.     "Resume" and the scenario is frozen until the button is clicked.
  14.  
  15.     The module is unexciting; it's meant as a basic illustration, rather
  16.     than anything useful.  There are lots of bells and whistles that
  17.     could be added.
  18.  
  19.     26 July 1986        Paul DuBois
  20. */
  21.  
  22.  
  23. # include    "BlobDemo.h"
  24. # include    <ControlMgr.h>
  25.  
  26.  
  27. # define    nStates        13
  28.  
  29. # define    hState1        220
  30. # define    hState2        110
  31. # define    vState        16
  32. # define    stateXOff    10
  33. # define    stateYOff    5
  34. # define    hCapital    85
  35. # define    vCapital    16
  36. # define    capXOff        225
  37. # define    capYOff        5
  38.  
  39.  
  40. struct info
  41. {
  42.     char    *stateName;
  43.     char    *capName;
  44. };
  45.  
  46.  
  47.  
  48. static GrafPtr            statesPort;
  49. static BlobSetHandle    donors;        /* donor blobs */
  50. static BlobSetHandle    receptors;    /* receptor blobs */
  51. static ControlHandle    giveUp;
  52. static Boolean            paused = false;
  53.  
  54. static struct info        scPair[nStates] =
  55. {
  56.     { "\pConnecticut",        "\pHartford" },
  57.     { "\pDelaware",            "\pDover" },
  58.     { "\pGeorgia",            "\pAtlanta" },
  59.     { "\pMaryland",            "\pAnnapolis" },
  60.     { "\pMassachusetts",    "\pBoston" },
  61.     { "\pNew Hampshire",    "\pConcord" },
  62.     { "\pNew Jersey",        "\pTrenton" },
  63.     { "\pNorth Carolina",    "\pRaleigh" },
  64.     { "\pNew York",            "\pAlbany" },
  65.     { "\pPennsylvania",        "\pHarrisburg" },
  66.     { "\pRhode Island",        "\pProvidence" },
  67.     { "\pSouth Carolina",    "\pColumbia" },
  68.     { "\pVirginia",            "\pRichmond" }
  69. };
  70.  
  71.  
  72. static Activate (active)
  73. Boolean    active;
  74. {
  75.     if (active)
  76.     {
  77.         SetDragRects (statesPort);
  78.         SetBCPermissions (true, true, false, true, true);
  79.     }
  80. }
  81.  
  82.  
  83. static Update ()
  84. {
  85.     DrawControls (statesPort);
  86.     DrawBlobSet (receptors);
  87.     DrawBlobSet (donors);
  88. }
  89.  
  90.  
  91. static Mouse (pt, t, mods)
  92. Point    pt;
  93. long    t;
  94. int        mods;
  95. {
  96. BlobHandle    b, d;
  97. ControlHandle    ctl;
  98.  
  99.     if (FindControl    (pt, statesPort, &ctl))
  100.     {
  101.         if (TrackControl (ctl, pt, nil))
  102.         {
  103.             if (paused)        /* either all answers are correct, or we're */
  104.             {                /* showing the answer.  Either way, restore */
  105.                 ThawBlobSet (receptors);    /* to start state */
  106.                 ThawBlobSet (donors);
  107.                 ZUnglueGlobSet (receptors);
  108.                 ShuffleBlobSet (receptors);
  109.                 ShuffleBlobSet (donors);
  110.                 SetCTitle (giveUp, "\pGive Up?");
  111.                 ValidRect (&statesPort->portRect);
  112.                 paused = false;
  113.             }
  114.             else        /* user gives up - show answer */
  115.             {
  116.                 for (b = FirstBlob (receptors); b != nil; b = NextBlob (b))
  117.                     ZGlueGlob (FirstBMatch (b), b);
  118.                 FreezeBlobSet (receptors);
  119.                 FreezeBlobSet (donors);
  120.                 SetCTitle (giveUp, "\pResume");
  121.                 paused = true;
  122.             }
  123.         }
  124.     }
  125.     else
  126.     {
  127.         BlobClick (pt, t, donors, receptors);
  128.         if (!paused && BlobSetQuiet (receptors))    /* answers correct? */
  129.         {
  130.             FreezeBlobSet (receptors);
  131.             FreezeBlobSet (donors);
  132.             SetCTitle (giveUp, "\pResume");
  133.             paused = true;
  134.         }
  135.     }
  136. }
  137.  
  138.  
  139. /*
  140.     Make blobs
  141. */
  142.  
  143.  
  144. MakeBlobs ()
  145. {
  146. int            i;
  147. Rect        r, r2;
  148. BlobHandle    b1, b2;
  149.  
  150.     donors = NewBlobSet ();
  151.     receptors = NewBlobSet ();
  152.     for (i = 0; i < nStates; ++i)
  153.     {
  154.         b1 = NewBlob (donors, false, 1, false, 0L);
  155.         b2 = NewBlob (receptors, false, 0, true, 0L);
  156.         NewBlobMatch (b1, b2);        /* attach the answer */
  157.  
  158.         OpenBlob ();    /* draw donor blob */
  159.         SetRect (&r, 0, 0, hCapital, vCapital);
  160.         TextBox (&scPair[i].capName[1], (long) scPair[i].capName[0], &r, 1);
  161.         CloseRectBlob (b1, &r, &r);
  162.         MoveBlob (b1, inFullBlob,  capXOff, capYOff + (vCapital + 1) * i);
  163.  
  164.         OpenBlob ();
  165.         SetRect (&r, 0, 0, hState1, vState);
  166.         SetRect (&r2, 0, 0, hState2, vState);
  167.         TextBox (&scPair[i].stateName[1], (long) scPair[i].stateName[0], &r2, 1);
  168.         SetRect (&r2, 0, 0, hCapital, vCapital);
  169.         OffsetRect (&r2, hState2 + 5, 0);
  170.         EraseRect (&r2);
  171.         FrameRect (&r2);
  172.         CloseRectBlob (b2, &r2, &r);
  173.         MoveBlob (b2, inFullBlob,  stateXOff, stateYOff + (vState + 2) * i);
  174.     }
  175.     ShuffleBlobSet (donors);
  176.     EnableBlobSet (receptors);
  177.     EnableBlobSet (donors);
  178. }
  179.  
  180.  
  181. StatesInit    ()
  182. {
  183. Rect    r;
  184.  
  185.     SkelWindow (statesPort = GetDemoWind (statesWindRes),
  186.                 Mouse,            /* mouse clicks */
  187.                 nil,            /* key clicks */
  188.                 Update,            /* updates */
  189.                 Activate,        /* activate/deactivate events */
  190.                 nil,            /* close window */
  191.                 DoWClobber,        /* dispose of window */
  192.                 nil,            /* idle proc */
  193.                 false);            /* irrelevant, since no idle proc */
  194.  
  195. /*
  196.     Make blobs.  Generally have to make donors before receptors.
  197. */
  198.     MakeBlobs ();
  199.  
  200.     r = statesPort->portRect;
  201.     SetRect (&r, r.left + 5, r.bottom - 25, r.left + 75, r.bottom - 5);
  202.     giveUp = NewControl (statesPort, &r, "\pGive Up?", true, 0, 0, 0,
  203.                             pushButProc, nil);
  204.  
  205.     Update ();
  206.     ValidRect (&statesPort->portRect);
  207. }
  208.